package shop.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import shop.DAO.OffersDAO;
import shop.DAO.OrderDAO;
import shop.bean.OfferBean;
import shop.bean.UserBean;

//import shop.ShoppingBean;

/**
 * Servlet implementation class ServiceServlet
 */
public class ServiceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ServiceServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void processRequest(HttpServletRequest request, 
			HttpServletResponse response)
					throws ServletException, java.io.IOException {

		// this is the dispatcher in our application
		// all requests will go through it.

		// get access to the session and to the shoppingcart
		// Store the logged in username in the session
		// and get jdbc-URL provided in web.xml as init-parameter

		//HttpSession sess = request.getSession();

		//sess.setAttribute("currentUser", request.getRemoteUser());

		// find out what to do based on the attribute "action"
		// no action or show
		//if(request.getParameter("action") == null || 
		//request.getParameter("action").equals("show")){	
		// go to main page where offers are displayed
		request.getRequestDispatcher("order_main.jsp").forward(request,response);

		//}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO call Method that produces ArrayList of Books --> pass
		Collection<OfferBean> offers = OffersDAO.getOffers();

		//RequestDispatcher rd = null;
		//request.setAttribute(..);
		//getServletContext().getRequestDispatcher("order_main.jsp").forward().
		request.getRequestDispatcher("order_main.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();	 
		//logged in ?
		UserBean user = (UserBean) session.getAttribute("currentUser");
		String statusOrder = new String();
		if(user != null){	
			String currentUser = user.getUsername();
			Map<String, String[]> ordered = request.getParameterMap();
			for ( Iterator<Entry<String, String[]>> iter = ordered.entrySet().iterator(); iter.hasNext(); ) {
				Entry<String, String[]> entry = iter.next();
				try{
					int productId = Integer.parseInt(entry.getKey());
					int orderedQuantity = Integer.parseInt(entry.getValue()[0]);
					OfferBean o = OffersDAO.getOffer(productId);
					if(o != null){
						String productName = o.getName();
						int stockQuantity = OffersDAO.getQuantity(productId);
						if(stockQuantity > orderedQuantity){
							OrderDAO.placeOrder(currentUser,productId, stockQuantity, orderedQuantity);
							statusOrder = statusOrder + "Great, " + productName + " has been successfully ordered <br>";
						} else {
							statusOrder = statusOrder + "Unfortunately we only have " + stockQuantity  + " of " + productName + " in the stock so we did not order that product for you. <br>";
						}
					}
				}
				catch (Throwable theException){System.out.println(theException);}
				}
				System.out.println();
				request.setAttribute("statusOrder", statusOrder);
				request.getRequestDispatcher("order_status.jsp").forward(request,response);
		}else{
			System.out.println("No session object!");
			response.sendRedirect("login");
		}
	}

}
